﻿/**
@charttype 统计类型
@url 请求的数据地址
@element 文档对象（显示统计的容器）
@title 标题
 **/
function GetChart(charttype, url, element, title) {
    var _datasuore = [];//统计数据（[ {value:0, name:''}]）
    var _titleData = [];//标题数据
    var _x_Data = [];//x轴分类数据
    var _x_footData = [];//x轴分类
    var index = 0;
    var dom = document.getElementById(element);
    var myChart = echarts.init(dom);
    var option = "";
    $.ajax({
        type: "Post",
        url: url,
        data: "json",
        success: function (data) {
            if (charttype == "pie") {//饼图
                $(data).each(function (j, k) {
                    _datasuore.push({ value: k.Value, name: k.Key });
                    _titleData.push(k.Key);
                });
                option = {
                    title: {
                        text: title,
                        //subtext: '纯属虚构',
                        x: 'center'
                    },
                    tooltip: {
                        trigger: 'item',
                        formatter: "{a} <br/>{b} : {c} ({d}%)"
                    },
                    legend: {
                        orient: 'vertical',
                        x: 'left',
                        data: _titleData
                    },
                    calculable: true,
                    series: [
                        {
                            name: title,
                            type: charttype,
                            radius: '55%',
                            center: ['50%', '60%'],
                            data: _datasuore
                        }
                    ]
                };
            } else if (charttype == "bar" || charttype == "line") {//折线图或者柱状图
                $(data).each(function (j, k) {
                    _titleData.push(k.Key);
                    var list = [];
                    $(k.Data).each(function (i, a) {
                        list.push(a.Value);
                        if (index == 0) {//只取一次X轴数据
                            _x_footData.push(a.Key);
                        }
                    });
                    _x_Data.push({
                        name: k.Key,
                        type: charttype,
                        data: list,
                        markPoint: {
                            data: [
                                { type: 'max', name: '最大值' },
                                { type: 'min', name: '最小值' }
                            ]
                        }
                    });
                    index++;
                });
                option = {
                    title: {
                        text: title
                        //subtext: '纯属虚构'
                    },
                    tooltip: {
                        trigger: 'axis'
                    },
                    legend: {
                        data: _titleData
                    },
                    calculable: true,
                    xAxis: [
                       {
                           type: 'category',
                           data: _x_footData
                       }
                    ],
                    yAxis: [
                         {
                             type: 'value'
                         }
                    ],
                    series: _x_Data
                };
            }
            //使用刚才的配置
            myChart.setOption(option);
        },
        error: function (e) {
            alert("出错了,请稍后再试!");

        }
    });
}